/*
* Copyright 2010-2011 Research In Motion Limited.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

function dataOnTheFly_onScreenReadySetting(element) {
    // Make the proper activity indicator appear
    if (bb.device.isBB10) {
        element.getElementById('bb7Loading').style.display = 'none';
    } else {
        element.getElementById('bb10Loading').style.display = 'none';
    }
}

function dataOnTheFly_initialLoadSetting(element) {
        
    //var  option, addPanel = document.getElementById('gradePanel');
    var gradeListPanel = document.getElementById('gradePanel'),
    curItems = gradeListPanel.getItems();
    var items = [],
    gradeItem;
    // Create our list item
    //DivGradePanel
    //<div data-bb-type="item" onbtnclick="document.getElementById('editScale').show();" data-bb-title="A+"</div>

    gradeItem = document.createElement('div');
    gradeItem.setAttribute('data-bb-type', 'item');
    gradeItem.setAttribute('id', 'A+');
    gradeItem.setAttribute('data-bb-title', 'A+');
    gradeItem.onbtnclick = pushScreen;
    gradeItem.innerHTML ='Value: '+localStorage.getItem('A+');
    items.push(gradeItem);
    gradeListPanel.appendItem(gradeItem);
    
    gradeItem = document.createElement('div');
    gradeItem.setAttribute('data-bb-type', 'item');
    gradeItem.setAttribute('id', 'A');
    gradeItem.setAttribute('data-bb-title', 'A');
    gradeItem.onbtnclick = pushScreen;
    gradeItem.innerHTML = 'Value: '+localStorage.getItem('A');
    items.push(gradeItem);
    gradeListPanel.insertItemBefore(gradeItem, curItems[0]);
    gradeItem = document.createElement('div');
    gradeItem.setAttribute('data-bb-type', 'item');
    gradeItem.setAttribute('id', 'A-');
    gradeItem.setAttribute('data-bb-title', 'A-');
    gradeItem.onbtnclick = pushScreen;
    gradeItem.innerHTML = 'Value: '+localStorage.getItem('A-');
    items.push(gradeItem);
    gradeListPanel.insertItemBefore(gradeItem, curItems[0]);
    gradeItem = document.createElement('div');
    gradeItem.setAttribute('data-bb-type', 'item');
    gradeItem.setAttribute('id', 'B+');
    gradeItem.setAttribute('data-bb-title', 'B+');
    gradeItem.onbtnclick = pushScreen;
    gradeItem.innerHTML = 'Value: '+localStorage.getItem('B+');
    items.push(gradeItem);
    gradeListPanel.insertItemBefore(gradeItem, curItems[0]);
    gradeItem = document.createElement('div');
    gradeItem.setAttribute('data-bb-type', 'item');
    gradeItem.setAttribute('id', 'B');
    gradeItem.setAttribute('data-bb-title', 'B');
    gradeItem.onbtnclick = pushScreen;
    gradeItem.innerHTML = 'Value: '+localStorage.getItem('B');
    items.push(gradeItem);
    gradeListPanel.insertItemBefore(gradeItem, curItems[0]);
    gradeItem = document.createElement('div');
    gradeItem.setAttribute('data-bb-type', 'item');
    gradeItem.setAttribute('id', 'B-');
    gradeItem.setAttribute('data-bb-title', 'B-');
    gradeItem.onbtnclick = pushScreen;
    gradeItem.innerHTML = 'Value: '+localStorage.getItem('B-');
    items.push(gradeItem);
    gradeListPanel.insertItemBefore(gradeItem, curItems[0]);
    gradeItem = document.createElement('div');
    gradeItem.setAttribute('data-bb-type', 'item');
    gradeItem.setAttribute('id', 'C+');
    gradeItem.setAttribute('data-bb-title', 'C+');
    gradeItem.onbtnclick = pushScreen;
    gradeItem.innerHTML = 'Value: '+localStorage.getItem('C+');
    items.push(gradeItem);
    gradeListPanel.insertItemBefore(gradeItem, curItems[0]);
    gradeItem = document.createElement('div');
    gradeItem.setAttribute('data-bb-type', 'item');
    gradeItem.setAttribute('id', 'C');
    gradeItem.setAttribute('data-bb-title', 'C');
    gradeItem.onbtnclick = pushScreen;
    gradeItem.innerHTML = 'Value: '+localStorage.getItem('C');
    items.push(gradeItem);
    gradeListPanel.insertItemBefore(gradeItem, curItems[0]);
    gradeItem = document.createElement('div');
    gradeItem.setAttribute('data-bb-type', 'item');
    gradeItem.setAttribute('id', 'D');
    gradeItem.setAttribute('data-bb-title', 'D');
    gradeItem.onbtnclick = pushScreen;
    gradeItem.innerHTML = 'Value: '+localStorage.getItem('D');
    items.push(gradeItem);
    gradeListPanel.insertItemBefore(gradeItem, curItems[0]);
    gradeItem = document.createElement('div');
    gradeItem.setAttribute('data-bb-type', 'item');
    gradeItem.setAttribute('id', 'E');
    gradeItem.setAttribute('data-bb-title', 'E');
    gradeItem.onbtnclick = pushScreen;
    gradeItem.innerHTML = 'Value: '+localStorage.getItem('E');
    items.push(gradeItem);
    gradeListPanel.insertItemBefore(gradeItem, curItems[0]);
    gradeItem = document.createElement('div');
    gradeItem.setAttribute('data-bb-type', 'item');
    gradeItem.setAttribute('id', 'F');
    gradeItem.setAttribute('data-bb-title', 'F');
    gradeItem.onbtnclick = pushScreen;
    if(localStorage.getItem('F').value == null){
        gradeItem.innerHTML = 'Value: -';
    }
    else{
        gradeItem.innerHTML = 'Value: '+localStorage.getItem('F');
    }
    items.push(gradeItem);
    gradeListPanel.insertItemBefore(gradeItem, curItems[0]);
    
    //    if (curItems.length > 0) {
    //        gradeListPanel.insertItemBefore(gradeItem, curItems[0]);
    //    } else {
    //        gradeListPanel.appendItem(gradeItem);
    //    }
    document.getElementById('gradePanel').refresh(gradeListPanel);

    // re-compute the scrolling area
    if (bb.scroller) {
        bb.scroller.refresh();
    }
    // Remove our waiting and refresh the list
    document.getElementById('waiting').style.display = 'none';
}

function pushScreen(){
    selected = document.getElementById('gradePanel').selected;

    //remove previous input field, prevent duplication 
    inputBefore = document.getElementById('tempInputScaleBefore').innerHTML;
    var input = document.getElementById('inputNewValue'+inputBefore);
    var button = document.getElementById('editButton'+inputBefore);
    if (input) {
        input.remove();
    }
    if (button) {
        button.remove();
    }
    
    document.getElementById('editScale').show();
    var addPanel = document.getElementById('editScale');
    
    //    <input type="text" value=""/> 
    //        <div data-bb-type="button" data-bb-style="stretch" onclick="document.getElementById('editScale').hide();">Save!</div>
    // Define label
    document.getElementById('labelEditScale').innerHTML = "Insert the new value for Grade <i>"+selected.getTitle()+"</i>";
    //document.getElementById('editScale').innerHTML = "<div data-bb-type=\"label\" id=\"labelEditScale\">Insert the new grade scale :</div><input type=\"text\" value=\"\" />";

    //Input field
    inputCourseName = document.createElement('input');
    inputCourseName.setAttribute('type','text');
    inputCourseName.setAttribute('id', 'inputNewValue'+selected.getTitle());
    inputCourseName.setAttribute('value', localStorage.getItem(selected.getTitle()));
    inputCourseName = bb.textInput.style(inputCourseName);
    addPanel.appendChild(inputCourseName);

    //Button Add New Course
    buttonAdd = document.createElement('div');
    buttonAdd.innerHTML = 'Edit Grade Value';
    buttonAdd.setAttribute('data-bb-type', 'button');
    buttonAdd.setAttribute('id', 'editButton'+selected.getTitle());
    buttonAdd.setAttribute('data-bb-style', 'stretch');
    buttonAdd.setAttribute('onclick', "editGrade();");
    buttonAdd = bb.button.style(buttonAdd);
    addPanel.appendChild(buttonAdd);
    
    // re-compute the scrolling area
    if (bb.scroller) {
        bb.scroller.refresh();
    }
    
    //set Temporary selected input before
    document.getElementById('tempInputScaleBefore').innerHTML = selected.getTitle();

//<div data-bb-type="button" data-bb-style="stretch" onclick="document.getElementById('editScale').hide();">Save!</div>
        
}

function editGrade(){
    selected = document.getElementById('gradePanel').selected;
    var newValue = document.getElementById('inputNewValue'+selected.getTitle());
    if (newValue) {
        newValue = newValue.value;
    //newValue = parseFloat(newValue.toFixed(2));
    }
    localStorage.setItem(selected.getTitle(), newValue);
    // re-compute the scrolling area
    gradeItem = document.createElement('div');
    gradeItem.setAttribute('data-bb-type', 'item');


    newEditedItem = document.getElementById(selected.getTitle());
    newEditedItem.innerHTML = 'Grade: '+selected.getTitle()+'  |  Value: '+localStorage.getItem(selected.getTitle());
    bb.refresh();
    alert("Edit Successfully. Grade: "+selected.getTitle()+" | Value: "+localStorage.getItem(selected.getTitle()));
}